package com.example.shopping_mall.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.bean.BrandBean;
import com.example.shopping_mall.bean.UiBrandDetail;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

//品牌详情页面
public class BrandDetailAdapter extends RecyclerView.Adapter<BrandDetailAdapter.BaseViewHolder>{

    private static final String TAG = BrandDetailAdapter.class.getSimpleName();
    Context context;
    private List<BrandBean> detailList;

    public BrandDetailAdapter(Context context, List<BrandBean> detailList) {
        this.context = context;
        this.detailList = detailList;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new DetailHolder(inflate(parent,R.layout.brand_detail_list_item));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        holder.bindData(detailList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return null==detailList?0:detailList.size();
    }

    private View inflate(ViewGroup parent, int layout) {
        return LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
    }

    public abstract class BaseViewHolder<DATA> extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindData(DATA data, int position);
    }

    public class DetailHolder extends BaseViewHolder<BrandBean>{

        TextView detailName;
        ImageView detailImage;
        TextView detailStory;

        public DetailHolder(View itemView) {
            super(itemView);

            detailName=itemView.findViewById(R.id.recommend_detail_name);
            detailImage=itemView.findViewById(R.id.recommend_detail_image);
            detailStory=itemView.findViewById(R.id.recommend_detail_story);
        }

        @Override
        public void bindData(BrandBean brandBean, int position) {
        detailName.setText(brandBean.getName());
        detailStory.setText(brandBean.getBrandStory());

        new Thread(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap;
                try {
                    URL url = new URL(brandBean.getBigPic());
                    //Log.d(TAG, "图片"+beans[finalI].getPic());
                    //创建连接方法
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //读取超时时长，即链接成功后，服务器返回时间
                    connection.setReadTimeout(5000);
                    //连接超时时长，连接服务器超时时间
                    connection.setConnectTimeout(5000);
                    //设置连接
                    connection.connect();
                    //返回结果码
                    //Log.d(TAG, "1111111111");
                    //final int responseCode = connection.getResponseCode ( );
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        InputStream inputStream = connection.getInputStream();//得到数据流
                        bitmap = BitmapFactory.decodeStream(inputStream);//将流转为bitmap

                        Handler handler=new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                detailImage.setImageBitmap(bitmap);
                            }
                        });
                    } else {
                        Log.d(TAG, "抱歉，出错了");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        }
    }
}
